// 通用函数
import useClipboard from 'vue-clipboard3';
import {ElMessage, ElMessageBox, ElNotification} from 'element-plus';
import {formatDate} from '/@/utils/formatTime';
import {useI18n} from 'vue-i18n';
import {Local} from "/@/utils/storage";
import request from "/@/utils/request";

const debug = true;

// 是否是调试模式
const log = function () {
    if (debug) {
        const args: any = arguments;
        window.console.log('>>>>>>>>>>>>Begin:' + new Date().toLocaleString() + '<<<<<<<<<<<<');
        var len = args.length;
        for (var i = 0; i < len; i++) {
            window.console.log(args[i]);
        }
        window.console.log('>>>>>>>>>>>>End<<<<<<<<<<<<');
    }
}
//验证是否为空
const isEmpty = (value: any) => {
    if (value === '' || value == null || value == undefined) {
        return true
    }
    return false
}

//如果为空反回指定内容
const ifEmpty = (validValue: any, refValue: any) => {
    if (isEmpty(validValue)) {
        return refValue
    }
    return validValue
}

const alert = {
    success: function (message: string) {
        ElMessage({
            message,
            type: 'success',
        })
    },
    info: function (message: string) {
        ElMessage({
            message
        })
    },
    warning: function (message: string) {
        ElMessage({
            message,
            type: 'warning',
        })
    },
    error: function (message: string) {
        ElMessage.error(message)
    }
}
//消息通知
const notification = {
    //自定义
    custom: (options: object) => {
        let dfop = {
            title: '',
            message: '',
            type: {
                type: String,
                default: 'default'
            }
        }
        Object.assign(dfop, options)
        ElNotification({
            title: dfop.title,
            message: dfop.message,
            type: dfop.type,
        })

    },
    success: (message: string) => {
        ElNotification({
            title: '成功',
            message,
            type: 'success',
        })
    },
    warning: (message: string) => {
        ElNotification({
            title: '警告',
            message,
            type: 'warning',
        })
    },
    error: (message: string) => {
        ElNotification({
            title: '错误',
            message,
            type: 'error',
        })
    },
    info: (message: string) => {
        ElNotification({
            title: '消息',
            message,
            type: 'info',
        })
    },

}

//消息确认框
const confirm = (message: string) => {
    return ElMessageBox.confirm(message, '提示', {
        type: 'warning',
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        closeOnClickModal: false,
        closeOnPressEscape: false,
        dangerouslyUseHTMLString: true
    })

}


const dictionaryInit = () => {

    let dictionaryAllData = Local.get("dictionaryData")
    if (dictionaryAllData == null) {
        request({
            url: '/api/system/dictionary/content/data/modelMap',
            method: 'get',
            params: {},
        }).then(res => {
            if (res.data.version) {
                Local.set("dictionaryData", res.data.data);
                dictionaryAllData = res.data.data
            }
        }).catch(error => {
            log('字典接口异常')
        })
    }
}

const form = {
    saveForm: () => {

    }
}

export default {
    confirm,
    debug,
    log,
    isEmpty,
    ifEmpty,
    alert,
    dictionaryInit,
}